﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Data;

/// <summary>
/// Summary description for DicionarioDeDados
/// </summary>
public class DicionarioDeDados
{
    ConexaoMDBD Command = new ConexaoMDBD();

    public DicionarioDeDados()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataSet dsDescricaoTabela(String cmd, Int32 COD_TAB)
    {
        OracleCommand cmdOP = Command.cmdMDBD(cmd);

        cmdOP.Parameters.Add(new OracleParameter("V_COD_TAB", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_TAB;
        cmdOP.Parameters.Add(new OracleParameter("V_RESULTADO_SAIDA", OracleDbType.RefCursor, ParameterDirection.Output));

        OracleDataAdapter daResultado = new OracleDataAdapter();
        daResultado.SelectCommand = cmdOP;

        DataSet dsResultado = new DataSet();
        daResultado.Fill(dsResultado, "Resultado");

        cmdOP.Connection.Dispose();
        cmdOP.Connection.Close();

        return dsResultado;
    }

    public DataSet dsListaColunas(String cmd, Int32 COD_TAB, String COD_FASE, String IND_TRAB)
    {
        OracleCommand cmdOP = Command.cmdMDBD(cmd);

        cmdOP.Parameters.Add(new OracleParameter("V_COD_TAB", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_TAB;
        cmdOP.Parameters.Add(new OracleParameter("V_COD_FASE", OracleDbType.Char, 1, ParameterDirection.Input)).Value = COD_FASE == "T" ? null : COD_FASE;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_TRAB", OracleDbType.Char, 1, ParameterDirection.Input)).Value = IND_TRAB == "T" ? null : IND_TRAB;

        cmdOP.Parameters.Add(new OracleParameter("V_RESULTADO_SAIDA", OracleDbType.RefCursor, ParameterDirection.Output));

        OracleDataAdapter daColunas = new OracleDataAdapter();
        daColunas.SelectCommand = cmdOP;

        DataSet dsColunas = new DataSet();
        daColunas.Fill(dsColunas, "Resultado");

        cmdOP.Connection.Dispose();
        cmdOP.Connection.Close();

        return dsColunas;
    }

    public DataSet dsListaTabelas(String cmd, DropDownList ddl, String DataValueField, String DataTextField, String Parametro01, String Parametro02, String Parametro03, String Parametro04, String Parametro05, String Parametro06)
    {
        OracleCommand cmdOP = Command.cmdMDBD(cmd);

        cmdOP.Parameters.Add(new OracleParameter("V_SISTEMA", OracleDbType.Varchar2, 255, ParameterDirection.Input)).Value = Parametro01;
        cmdOP.Parameters.Add(new OracleParameter("V_COD_FASE_TAB", OracleDbType.Char, 1, ParameterDirection.Input)).Value = Parametro02 == "T" ? null : Parametro02;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_TRAB_TAB", OracleDbType.Char, 1, ParameterDirection.Input)).Value = Parametro03 == "T" ? null : Parametro03;
        cmdOP.Parameters.Add(new OracleParameter("V_COD_FASE_COLU", OracleDbType.Char, 1, ParameterDirection.Input)).Value = Parametro04 == "T" ? null : Parametro04;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_TRAB_COLU", OracleDbType.Char, 1, ParameterDirection.Input)).Value = Parametro05 == "T" ? null : Parametro05;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_INDX", OracleDbType.Char, 1, ParameterDirection.Input)).Value = Parametro06;

        cmdOP.Parameters.Add(new OracleParameter("V_RESULTADO_SAIDA", OracleDbType.RefCursor, ParameterDirection.Output));

        OracleDataAdapter daResultado = new OracleDataAdapter();
        daResultado.SelectCommand = cmdOP;

        DataSet dsResultado = new DataSet();

        daResultado.Fill(dsResultado, "Resultado");

        ddl.DataSource = dsResultado.Tables["Resultado"];
        ddl.DataValueField = DataValueField;
        ddl.DataTextField = DataTextField;
        ddl.DataBind();

        cmdOP.Connection.Dispose();
        cmdOP.Connection.Close();

        return dsResultado;
    }

    public Int32 SalvarTabela(String cmd, Int32 COD_TAB, Int32 COD_PESS, String DESC_TAB, String IND_VALD)
    {
        OracleCommand cmdOP = Command.cmdMDBD(cmd);

        cmdOP.Parameters.Add(new OracleParameter("V_COD_TAB", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_TAB;
        cmdOP.Parameters.Add(new OracleParameter("V_COD_PESS", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_PESS;
        cmdOP.Parameters.Add(new OracleParameter("V_DESC_TAB", OracleDbType.Varchar2, 4000, ParameterDirection.Input)).Value = DESC_TAB;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_VALD", OracleDbType.Char, 1, ParameterDirection.Input)).Value = IND_VALD;
        cmdOP.Parameters.Add(new OracleParameter("V_RESULTADO_SAIDA", OracleDbType.RefCursor, ParameterDirection.Output));

        OracleDataAdapter daResultado = new OracleDataAdapter();
        daResultado.SelectCommand = cmdOP;

        DataSet dsResultado = new DataSet();
        daResultado.Fill(dsResultado, "Resultado");

        cmdOP.Connection.Dispose();
        cmdOP.Connection.Close();

        Int32 SalvarTabela = 0;

        foreach (DataRow drResultado in dsResultado.Tables[0].Rows)
        {
            SalvarTabela = Int32.Parse(drResultado[0].ToString());
        }

        return SalvarTabela;
    }

    public Int32 SalvarColuna(String cmd, Int32 COD_COLU, Int32 COD_PESS, String DESC_COLU, String IND_VALD)
    {
        OracleCommand cmdOP = Command.cmdMDBD(cmd);

        cmdOP.Parameters.Add(new OracleParameter("V_COD_COLU", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_COLU;
        cmdOP.Parameters.Add(new OracleParameter("V_COD_PESS", OracleDbType.Int32, ParameterDirection.Input)).Value = COD_PESS;
        cmdOP.Parameters.Add(new OracleParameter("V_DESC_COLU", OracleDbType.Varchar2, 4000, ParameterDirection.Input)).Value = DESC_COLU;
        cmdOP.Parameters.Add(new OracleParameter("V_IND_VALD", OracleDbType.Char, 1, ParameterDirection.Input)).Value = IND_VALD;
        cmdOP.Parameters.Add(new OracleParameter("V_RESULTADO_SAIDA", OracleDbType.RefCursor, ParameterDirection.Output));

        OracleDataAdapter daResultado = new OracleDataAdapter();
        daResultado.SelectCommand = cmdOP;

        DataSet dsResultado = new DataSet();
        daResultado.Fill(dsResultado, "Resultado");

        cmdOP.Connection.Dispose();
        cmdOP.Connection.Close();

        Int32 SalvarColuna = 0;

        foreach (DataRow drResultado in dsResultado.Tables[0].Rows)
        {
            SalvarColuna = Int32.Parse(drResultado[0].ToString());
        }

        return SalvarColuna;
    }
}